<?php

namespace App\Services;

use App\Http\Resources\Mini\Shop\KeywordsCollection;
use App\Http\Resources\Mini\Shop\KeywordsHistoryCollection;

/**
 * Class Keywords
 * @package App\Services
 * 2021/3/18 - 10:49
 * Administrator
 *
 */
class Keywords extends BaseService
{
    public $keywordsModel;
    public $HistoryService;

    public function __construct()
    {
        $this->keywordsModel = new \App\Models\Keywords();
        $this->HistoryService = new KeywordsHistory();
    }

    /**
     * 2021/3/18--10:50
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:getKeywordsList
     * @effect:关键词列表
     */
    public function getKeywordsList()
    {

        $userService = new UserService();
        $userInfo = $userService->getUserInfo();

        $keyword = $this->keywordsModel->orderBy('search_num', 'desc')->limit(15)->get();
        if ($userInfo) {
            $history = $this->HistoryService->getModel()->where('user_id', $userInfo['id'])->orderBy('search_num', 'desc')->limit(15)->get();
        } else {
            $history = [];
        }
        $data = [
            'keyword' => new KeywordsCollection($keyword),
            'history' => new KeywordsHistoryCollection($history)
        ];
        return $this->format($data);
    }

    /**
     * 2021/3/18--10:50
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:addKeywords
     * @effect:添加关键词
     */
    public function addKeywords($keyword)
    {
        $userService = new UserService();
        $userInfo = $userService->getUserInfo();
        $is_exit = $this->keywordsModel->where('keyword', $keyword)->first();
        if ($is_exit) {
            $is_exit->search_num = $is_exit->search_num + 1;
            $is_exit->updated_at = now()->timestamp;
            $is_exit->save();
            if ($userInfo) {
                $this->HistoryService->addHistory($keyword, $userInfo['id']);
            }
            return $this->format([]);
        }
        $insert = [
            'keyword' => $keyword,
            'search_num' => 1,
            'type' => 1,
            'created_at' => now()->timestamp
        ];
        $this->keywordsModel->insert($insert);
        //如果用户存在则给用户添加记录

        if ($userInfo) {
            $this->HistoryService->addHistory($keyword, $userInfo['id']);
        }
        return $this->format([]);
    }


    /**
     * 2021/3/18--14:06
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:clearHistory
     * @effect:清空搜索历史
     */
    public function clearHistory()
    {
        try {
            $userService = new UserService();
            $userInfo = $userService->getUserInfo();
            $this->HistoryService->getModel()->where('user_id', $userInfo['id'])->delete();
            return $this->format([]);
        } catch (\Exception $e) {
            return $this->format_error();
        }
    }

    /**
     *析构方法
     */
    public function __destruct()
    {
        $this->keywordsModel;
    }
}
